﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Text;
using System.Net;
using System.IO;

namespace Touch
{
    public partial class _MoreApps : System.Web.UI.Page
    {
        LocalFunctions FUNC;
        protected string sAppsList = string.Empty;

        private PageType _pageType;
        private int iCurrentPage;
        private int iCatID;
        private int iStartNum;
        private string _SearchTerm;
        private bool isTop;
        private int iPageSize = 5;

        protected void Page_Load(object sender, EventArgs e)
        {
            FUNC = new LocalFunctions();
            GetParameters();
            List<Application> Items = null;
            ArrayList arrCatID ;
            int TotalResults = 0;

            #region Get items by type
            switch (_pageType)
            {
                case PageType.Regular:
                    Items = FUNC.GetAppsByCatID(iCatID, iStartNum, iPageSize, out TotalResults);
                    break;

                case PageType.Reccomended:
                    Items = FUNC.GetRecomendedApps(iStartNum, iPageSize, out TotalResults);
                    break;
                case PageType.Newest:
                    Items = FUNC.GetNewApps(iStartNum, iPageSize, out TotalResults);
                    break;
                case PageType.Popular:
                    Items = FUNC.GetPopularApps(iStartNum, iPageSize, out TotalResults);
                    break;
                case PageType.Search:
                    Items = FUNC.SearchApps(_SearchTerm, iStartNum, iPageSize, out TotalResults);
                    break;
            }
            #endregion

            Boolean isOrange = true;

            if (iCurrentPage == 0)
                sAppsList += "totalItems:" + TotalResults;
            foreach (Application item in Items)
            {
                bool bHaveOrange = false;
               
                string sAppName = item.name;
                if (item.price != null)
                {
                    string sPrice = item.price.ToString();// +" ש\"ח";
                    if (!isOrange)
                    {
                        sAppName = item.name.Insert(0, "*");
                        sPrice = "באתר הספק";
                    }
                    else
                    {
                        if (item.ismonthly == 1)
                        {
                            sPrice = "תשלום חודשי";
                        }
                    }
                }
                string sAltText = item.name;
                StringBuilder sb = new StringBuilder();

                sb.AppendFormat("<li><a class=\"item\" href=\"AppDetails.aspx?aid={0}&cid={1}\">",item.id,iCatID,iCatID);
				sb.AppendFormat("<img src=\"{0}\" class=\"thumb\" />",item.icon);
				sb.AppendFormat("<div class=\"like\">{0}</div>",item.Likes);
				//sb.Append("<%--<div class=\"pick\">בחירת העורך</div>--%>");
				sb.AppendFormat("<h3>{0}</h3>",item.name);
				if (item.IsLoto)
                    sb.Append("<div Class=\"orange-note\">משתתף בהגרלה</div>");
				if (!item.IsOrange)
                    sb.Append("<div Class=\"orange-note\" style=\"color:Black\">להורדה מאתר חיצוני*</div>");
				
                sb.AppendFormat("<span class=\"price\">מחיר {0}</span></a></li>",item.price);

                sAppsList += sb.ToString();
            }
        }

        private void GetParameters()
        {
            iCatID = Convert.ToInt32(Request.QueryString["id"]);
            isTop = Convert.ToBoolean(Convert.ToInt32(Request.QueryString["Top"]));
            iCurrentPage = Convert.ToInt32(Request.QueryString["page"]);
            iPageSize = Convert.ToInt32(Request.QueryString["pagesize"]);
            iStartNum = ((iCurrentPage > 0) ? iCurrentPage : 0) * iPageSize;
            _pageType = (PageType)Convert.ToInt32(Request.QueryString["type"]);
            _SearchTerm = (Request.QueryString["str"] != null) ? Request.QueryString["str"].ToString() : string.Empty;
        }
    }
}
